define(['core', 'layer'], function (c, layer) {
    var ldp = window.LODOP
    function checkLodop() {
        ldp = ldp || window.LODOP
        if (ldp) return true
        setTimeout(function () {
            ldp = window.LODOP
        }, 100)
        layer.open({ content: '未检测到打印插件，请确认是否已经安装(CLodop_Setup_for_Win32NT.exe)！', btn: '好的' })
        throw new Error('未检测到打印插件!')
    }

    function init(name) {
        checkLodop()
        ldp.PRINT_INIT(name || 'label_print')
        return ldp
    }

    function setPrintPageSize(orient, width, height) {
        ldp.SET_PRINT_PAGESIZE(orient, width || 2800, height || 800, "标签纸")
    }

    function setPrintStyle(attr, value) {
        ldp.SET_PRINT_STYLE(attr, value)
    }

    function setPrintFontName(index, fontName) {
        if (fontName) ldp.SET_PRINT_STYLEA(index, "FontName", fontName)
    }

    function setPrintFontSize(index, fontSize) {
        ldp.SET_PRINT_STYLEA(index, "FontSize", fontSize)
    }

    function addPrintText(x, y, width, height, text) {
        ldp.ADD_PRINT_TEXT(y, x, width, height, text)
    }

    function addPrintRect(x, y, width, height, lineStyle, lineWidth) {
        ldp.ADD_PRINT_RECT(x, y, width, height, lineStyle || 0, lineWidth || 1)
    }

    function addPrintBarcode(x, y, width, height, barcode) {
        ldp.ADD_PRINT_BARCODE(y, x, width || 130, height || 47, '128Auto', barcode || '1234567890');
    }

    function addPrintQrcode(x, y, width, height, content) {
        ldp.ADD_PRINT_BARCODE(y, x, width || 64, height || 64, 'QRCode', content || '未设置二维码内容');
    }

    function addPrintLine(x1, y1, x2, y2, lineStyle, lineWidth) {
        ldp.ADD_PRINT_LINE(y1, x1, y2, x2, lineStyle || 0, lineWidth || 1)
    }

    function addPrintImage(x, y, width, height, src) {
        ldp.ADD_PRINT_IMAGE(y, x, width, height, src)
    }

    function newPage() {
        ldp.NewPage()
    }

    function preview() {
        ldp.PREVIEW()
    }

    function print() {
        ldp.PRINT()
    }

    return {
        init: init,
        setPrintPageSize: setPrintPageSize,
        setPrintStyle: setPrintStyle,
        setPrintFontName: setPrintFontName,
        setPrintFontSize: setPrintFontSize,
        addPrintText: addPrintText,
        addPrintRect: addPrintRect,
        addPrintBarcode: addPrintBarcode,
        addPrintQrcode: addPrintQrcode,
        addPrintLine: addPrintLine,
        addPrintImage: addPrintImage,
        newPage: newPage,
        preview: preview,
        print: print
    }
})
